package com.gator.desig_mode.mvc2;


import java.util.ArrayList;
import java.util.List;


/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class DispatchServlet {

  //聚合HandlerAdapter
  List<HandlerAdapter> list;

  public DispatchServlet() {
    list = new ArrayList<>();
    list.add(new AnnotationAdapter());
    list.add(new HttpAdapter());
    list.add(new SimpleAdapter());
  }

  public void doDispatch() {
    //构建一个请求
    Handler handler = new AnnotationHandler();

    //根据请求获取适配器
    HandlerAdapter adapter = getAdapter(handler);
    //执行对应handler 方法
    adapter.doHandler(handler);


  }

  private HandlerAdapter getAdapter(Handler handler) {
    for (HandlerAdapter adapter : list
    ) {
      if (adapter.support(handler)) {
        return adapter;
      }
    }
    return null;
  }

  public static void main(String[] args) {
    DispatchServlet dispatchServlet = new DispatchServlet();
    dispatchServlet.doDispatch();
  }


}
